package com.itheima.controller;

import com.itheima.exception.BusinessException;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping("/depts")
    public Result selectDeptAll(){
        List<Dept> list = deptService.selectAll();
        return Result.success(list);
    }
    @DeleteMapping("/depts")
    public Result deleteById(@RequestParam("id") Integer id) {
        deptService.deletById(id);
        return Result.success();
    }
    @PostMapping("/depts")
    public Result addDept(@RequestBody Dept dept){
        deptService.add(dept);
        return Result.success();
    }
    //根据id查询
    @GetMapping("/depts/{id}")
    public Result selectById(@PathVariable Integer id){
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }
}
